//
//  ConsumerViewController.m
//  VidoarDemo
//
//  Created by xm on 2025/7/29.
//

#import "ConsumerViewController.h"
#import "AncsConsumerBleClient.h"

@interface ConsumerViewController ()

@property (nonatomic,strong) AncsConsumerBleClient *consumerClient;

@property(nonatomic, strong) UIButton *startButton;
@property(nonatomic, strong) UIButton *stopButton;
@property(nonatomic, strong) UIButton *sendButton;


@end

@implementation ConsumerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}
-(void) setup
{
    [self createdUI];
    [self watch];

}

-(void) createdUI
{
    [self.view addSubview:self.startButton];
    [self.view addSubview:self.stopButton];
    [self.view addSubview:self.sendButton];
}
-(void) watch
{
    [[self.startButton rac_signalForControlEvents:UIControlEventTouchDown] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self startAction];
       
    }];
    [[self.stopButton rac_signalForControlEvents:UIControlEventTouchDown] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self stopAction];
       
    }];
    [[self.sendButton rac_signalForControlEvents:UIControlEventTouchDown] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self sendAction];
       
    }];

    
}


#pragma mark - getter

- (UIButton *)startButton
{
    if(_startButton){
        return _startButton;
    }

    CGRect frame = CGRectMake(20, NavAndStatusToolHeight + 20, screenWidth - 40, 48);
    UIButton* button = [UIViewHelper createButtonWithTitle:@"Start" frame:frame];

    _startButton = button;
    return _startButton;
}

- (UIButton *)stopButton
{
    if(_stopButton){
        return _stopButton;
    }

    CGRect frame = CGRectMake(20, self.startButton.bottom+30, screenWidth - 40, 48);
    UIButton* button = [UIViewHelper createButtonWithTitle:@"Stop" frame:frame];

    _stopButton = button;
    return _stopButton;
}
- (UIButton *)sendButton
{
    if(_sendButton){
        return _sendButton;
    }

    CGRect frame = CGRectMake(20, self.stopButton.bottom+ 30, screenWidth - 40, 48);
    UIButton* button = [UIViewHelper createButtonWithTitle:@"Send" frame:frame];

    _sendButton = button;
    return _sendButton;
}


- (AncsConsumerBleClient *)consumerClient
{
    if(_consumerClient == nil){
        _consumerClient = [[AncsConsumerBleClient alloc] init];
    }
    
    return _consumerClient;
}

#pragma mark - actions

-(void) startAction
{
    self.log(@"startAction");
    [self.consumerClient start];
}

-(void) stopAction
{
    self.log(@"stopAction");
    
    [self.consumerClient stop];
}

-(void) sendAction
{
    self.log(@"sendAction");
    
//    NSString* str = @"1234567890";
//    NSData* data = [str dataUsingEncoding:NSUTF16StringEncoding];
//
    int32_t intValue = 0x3344;
    NSData* data = [NSData dataWithBytes:&intValue length:sizeof(intValue)];
    
    [self.consumerClient sendData:data];
}



#pragma mark - motheds




@end
